﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HelperPrototype._DB
{
    class NguoiDungDB
    {
        public static IQueryable selectAll()
        {
            _DB.DataClasses1DataContext db = new _DB.DataClasses1DataContext();
            var query = from u in db.NguoiDungs
                        where u.deleted ==false
                        select u;
            return query;
        }
        public static NguoiDung selectID(String id)
        {
            _DB.DataClasses1DataContext db = new _DB.DataClasses1DataContext();
            var query = from u in db.NguoiDungs
                        where u.MaNguoiDung==id
                        select u ;
            if (query.Count()>0)
            {
                return query.First();
            }
            return null;
            //return query;
        }
        public static bool insert(_DB.NguoiDung u)
        {
            _DB.DataClasses1DataContext db = new _DB.DataClasses1DataContext();
            
            db.NguoiDungs.InsertOnSubmit(u);
            db.SubmitChanges();
            return true;
        }
        public static NguoiDung update(NguoiDung newNguoiDung)
        {
            
            _DB.DataClasses1DataContext db = new _DB.DataClasses1DataContext();
            var query = (from u in db.NguoiDungs
                        where u.MaNguoiDung == newNguoiDung.MaNguoiDung
                        select u);
            if (query.Count() > 0)
            {
                NguoiDung usr = query.First();
                //usr.Username = newNguoiDung.Username;
                usr.Password = newNguoiDung.Password;
                usr.NhomNguoiDung_ID = newNguoiDung.NhomNguoiDung_ID;
                usr.MaPhongBan = newNguoiDung.MaPhongBan;
                usr.ViTriPhongBan = newNguoiDung.ViTriPhongBan;
                usr.Ten = newNguoiDung.Ten;
                usr.NgaySinh = newNguoiDung.NgaySinh;
                usr.Email = newNguoiDung.Email;
                usr.ThongTinNhanVien = newNguoiDung.ThongTinNhanVien;
                usr.TrangThai = newNguoiDung.TrangThai;
                usr.deleted = newNguoiDung.deleted;
                db.SubmitChanges();
              //  return query.First();
            }
            return null;
            //return query;
        }
        public static void delete(String id)
        {
            _DB.DataClasses1DataContext db = new _DB.DataClasses1DataContext();
            var query = from u in db.NguoiDungs
                        where u.MaNguoiDung == id
                        select u;
            if (query.Count() > 0)
            {
                NguoiDung usr = query.First();
                usr.deleted = true;
                db.SubmitChanges();
            }
            return ;
            //return query;
        }
    }

}
